/*
 * igo: com.cctv.igo.security.domain.Principal.java Create On 2011-3-15 下午04:04:12 By Q-Wang
 * $Revision: 1.1 $
 */
package com.ctvit.framework.security.domain;

import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.ctvit.framework.domain.BaseEntity;

/**
 * @author <a href="mailto:apeidou@gmail.com">Q-Wang</a>
 *
 */
@Entity
@Table(name="T_SEC_USERS")
@SequenceGenerator(name="S_SEC_USERS",sequenceName="S_SEC_USERS")
public class Principal implements BaseEntity<Long>, UserDetails, java.security.Principal {

	private Long id;
	private String email;
	private String nickName;
	private String password;
	private String type;
	private String creator;
	private Date createTimestamp;
	private String modifier;
	private Date modifyTimestamp;

	private boolean locked;
	private boolean expired;
	private boolean enabled;
	private boolean credentialsExpired;

	/**
	 * @return the id
	 * @see BaseEntity#getId()
	 */
	@Id
	@Column(name="PK")
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="S_SEC_USERS")
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 * @see BaseEntity#setId(java.io.Serializable)
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the email
	 */
	@Column(name="EMAIL",length=32,nullable=false,unique=true)
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the type
	 */
	@Column(name="TYPE",nullable=false,length=16)
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the creator
	 */
	@Column(name="CREATOR",nullable=false,length=32)
	public String getCreator() {
		return creator;
	}

	/**
	 * @param creator the creator to set
	 */
	public void setCreator(String creator) {
		this.creator = creator;
	}

	/**
	 * @return the createTimestamp
	 */
	@Column(name="CREATE_TIMESTAMP",nullable=false)
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateTimestamp() {
		return createTimestamp;
	}

	/**
	 * @param createTimestamp the createTimestamp to set
	 */
	public void setCreateTimestamp(Date createTimestamp) {
		this.createTimestamp = createTimestamp;
	}

	/**
	 * @return the modifier
	 */
	@Column(name="MODIFIER",length=32)
	public String getModifier() {
		return modifier;
	}

	/**
	 * @param modifier the modifier to set
	 */
	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	/**
	 * @return the modifyTimestamp
	 */
	@Column(name="MODIFY_TIMESTAMP")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getModifyTimestamp() {
		return modifyTimestamp;
	}

	/**
	 * @param modifyTimestamp the modifyTimestamp to set
	 */
	public void setModifyTimestamp(Date modifyTimestamp) {
		this.modifyTimestamp = modifyTimestamp;
	}

	/**
	 * @return the locked
	 */
	@Column(name="LOCKED")
	public boolean isLocked() {
		return locked;
	}

	/**
	 * @param locked the locked to set
	 */
	public void setLocked(boolean locked) {
		this.locked = locked;
	}

	/**
	 * @return the expired
	 */
	@Column(name="EXPIRED")
	public boolean isExpired() {
		return expired;
	}

	/**
	 * @param expired the expired to set
	 */
	public void setExpired(boolean expired) {
		this.expired = expired;
	}

	/**
	 * @return the credentialsExpired
	 */
	@Column(name="CREDENTIALS_EXPIRED")
	public boolean isCredentialsExpired() {
		return credentialsExpired;
	}

	/**
	 * @param credentialsExpired the credentialsExpired to set
	 */
	public void setCredentialsExpired(boolean credentialsExpired) {
		this.credentialsExpired = credentialsExpired;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @param enabled the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	/**
	 * @see java.security.Principal#getName()
	 */
	@Transient
	public String getName() {
		return email;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	@Transient
	public Collection<GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		return Collections.emptyList();
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#getPassword()
	 */
	@Column(name="PASSWORD",length=32,nullable=false)
	public String getPassword() {
		return password;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	@Transient
	public String getUsername() {
		return email;
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	@Transient
	public boolean isAccountNonExpired() {
		return !isExpired();
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	@Transient
	public boolean isAccountNonLocked() {
		return !isLocked();
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	@Transient
	public boolean isCredentialsNonExpired() {
		return !isCredentialsExpired();
	}

	/**
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	@Column(name="ENABLED")
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * @return the nickName
	 */
	@Column(name="NICK_NAME",length=64,nullable=true)
	public String getNickName() {
		return nickName;
	}

	/**
	 * @param nickName the nickName to set
	 */
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return super.toString();
	}

	public Criteria buildQuery(Criteria criteria) {
		if (id!=null) {
			criteria.add(Restrictions.idEq(id));
		}
		if (StringUtils.isNotEmpty(email)) {
			criteria.add(Restrictions.eq("email", email));
		}
		if (StringUtils.isNotEmpty(nickName)) {
			criteria.add(Restrictions.eq("nickName", nickName));
		}
		if (StringUtils.isNotEmpty(type)) {
			criteria.add(Restrictions.eq("type", type));
		}
		if (StringUtils.isNotEmpty(creator)) {
			criteria.add(Restrictions.eq("creator", creator));
		}
		if (createTimestamp!=null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(modifyTimestamp);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date from = calendar.getTime();
			calendar.add(Calendar.DATE, 1);
			Date to = calendar.getTime();
			criteria.add(Restrictions.between("createTimestamp", from, to));
		}
		if (StringUtils.isNotEmpty(modifier)) {
			criteria.add(Restrictions.eq("modifier", modifier));
		}
		if (modifyTimestamp!=null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(modifyTimestamp);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			Date from = calendar.getTime();
			calendar.add(Calendar.DATE, 1);
			Date to = calendar.getTime();
			criteria.add(Restrictions.between("modifyTimestamp", from, to));
		}
		return criteria;
	}
}
